package com.goddess.yeecolor.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.goddess.yeecolor.Modle.jincheng;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.interfaceAll.OnClickListener;
import com.goddess.yeecolor.tools.ImageUtils;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 瀑布流
 */
public class EnerciseItem3Adapter extends BaseRecyclerAdapter<EnerciseItem3Adapter.SimpleAdapterViewHolder> {
    private OnClickListener onClickListener;
    private List<jincheng> mList;
    private Context mContext;
    private Window window;
    public EnerciseItem3Adapter(List<jincheng>list, Context context,Window window){
        this.window=window;
        this.mContext=context;
        this.mList=list;
    }
    @Override
    public SimpleAdapterViewHolder getViewHolder(View view) {
        return new SimpleAdapterViewHolder(view, false);
    }

    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener=onClickListener;
    }
    @Override
    public SimpleAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_exerciseitem3,parent,false);
        SimpleAdapterViewHolder viewHolder=new SimpleAdapterViewHolder(view,true);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(SimpleAdapterViewHolder holder, int position, boolean isItem) {

        jincheng info=mList.get(position);
        ImageUtils.getInstance().ImageStart(mContext,info.getThumb(),holder.imageView);
//        setWidthHeight(holder.exerciseitem_card,info.getWidth()+10,info.getRate());
        setWidthHeight(holder.imageView,info.getWidth(),info.getHeight(),info.getRate());
        holder.title.setText(info.getTitle());
        holder.zan.setText(info.getZan());
        holder.share.setText(info.getShare());
        /**
         * 点赞
         */
        holder.exerciseitem3_linearzan.setOnClickListener((view)->{
            setRequest(info.getId());
            if (code==100){
                holder.zan.setText((Integer.valueOf(info.getZan())+1)+"");
            }
        });

    }

    @Override
    public int getAdapterItemCount() {
        return mList.size();
    }

    class SimpleAdapterViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private LinearLayout exerciseitem3_linearshare,exerciseitem3_linearzan;
        private TextView title,share,zan;
        private CardView exerciseitem_card;
        public SimpleAdapterViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if (isItem){
                imageView= (ImageView) itemView.findViewById(R.id.exerciseitem3_image);
                title= (TextView) itemView.findViewById(R.id.exerciseitem3_title);
                share= (TextView) itemView.findViewById(R.id.exerciseitem3_share);
                zan= (TextView) itemView.findViewById(R.id.exerciseitem3_zan);
                exerciseitem_card= (CardView) itemView.findViewById(R.id.exerciseitem_card);
                exerciseitem3_linearshare= (LinearLayout) itemView.findViewById(R.id.exerciseitem3_linearshare);
                exerciseitem3_linearzan= (LinearLayout) itemView.findViewById(R.id.exerciseitem3_linearzan);
                itemView.setOnClickListener((view)->{
                    onClickListener.OnClick(view,getLayoutPosition());
                });

            }

        }

    }

    private void setWidthHeight(View imageView,int widht,int heigh,double rate){
        Display display=window.getWindowManager().getDefaultDisplay();
        //获取手机分辨率
        DisplayMetrics displayMetrics=new DisplayMetrics();
        display.getMetrics(displayMetrics);//将宽高存储到display
        ViewGroup.LayoutParams params=imageView.getLayoutParams();
        params.width=widht;
        params.height=heigh;


    }

    private void setRequest(int id){
        Map<Object,Object> map=new HashMap<>();
        map.put("id",id+"");
        map.put("unique", SharePreceUtils.getOther("user","unique"));
        map.put("typed", MyUrl.TYPE);
//        LogUtils.getInstence().info(MyUrl.ZAN+"?id="+id+"&unique="+(SharePreceUtils.getOther("user","unique"))+"&typed="+MyUrl.TYPE);
        OkttpUtils.getIntence().OkttpPost(MyUrl.ZAN,map,handler,1);
    }
    private String jsonString;
    private int code;
    /**
     * 点赞
     */
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");
                JSONObject object=new JSONObject(jsonString);
                code=object.getInt("code");
                if (code==100){
                    ToastUtils.toast(object.getString("msg"));

                }
                else {
                    ToastUtils.toast(object.getString("msg"));
                }
            }
            catch (Exception e){

            }
        }
    };
}
